<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Search extends Backend
{
    //搜索会员
    public function member()
    {
        $search = request()->post('nickname');

        $members = Db::table('member')->field('id,nickname')->where('nickname', 'like', "%" . $search . "%")->limit(5)->select();

        $count = Db::table('member')->field('id,nickname')->where('nickname', 'like', "%" . $search . "%")->count();

        echo json_encode([
            'list' => $members,
            'total' => $count,
        ]);
    }

    //搜索产品
    public function product()
    {
        $search = request()->post('title');

        $members = Db::table('product')->field('id,title')->where('title', 'like', "%" . $search . "%")->where('stock', '>', 0)->limit(5)->select();

        $count = Db::table('product')->field('id,title')->where('title', 'like', "%" . $search . "%")->where('stock', '>', 0)->count();

        echo json_encode([
            'list' => $members,
            'total' => $count,
        ]);
    }
}
